﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class customerapp : System.Web.UI.Page
    {
        protected string[] customer_Data = new string[] { "", ""};
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_APP_INFO] != null)
            {
                string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
                customer_Data = GetOrdersTendency(30, appNum);
            }
            else
            {
                customer_Data = GetOrdersTendency(30,"");
            }
        }

        private string[] GetOrdersTendency(int nday, string appNum)
        {
            //获取最近n天的统计数据
            DateTime startday = DateTime.Now.AddDays(-nday);
            DateTime endDay = DateTime.Now.AddDays(-1);
            string strStartTime = startday.ToString("yyyy-MM-dd") + " 00:00:00.000";
            string strEndTime = endDay.ToString("yyyy-MM-dd") + " 23:59:59.999";

            BLL.statistics bll = new BLL.statistics();
            System.Data.DataSet dsCustomers = bll.GetCustomerTimesDataByAppNum(strStartTime,strEndTime);
            if (appNum!="")
            {
                dsCustomers = bll.GetCustomerTimesDataByAppNum(appNum, strStartTime, strEndTime);
            }

            string xAxis = string.Empty;
            for (int i = 0; i < nday; i++)
            {
                xAxis += "'" + startday.AddDays(i).ToString("MM-dd") + "',";
            }
            if (xAxis.Length > 0) 
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);
            }

            string [] arr_series = new string[2];
            string strCity = string.Empty;
            foreach (System.Data.DataRow r in dsCustomers.Tables[0].Rows) 
            {
                if (strCity.IndexOf(r["city"].ToString()) == -1) 
                {
                    strCity += "'" + r["city"].ToString() + "',";
                }
            }

            string yAxis = string.Empty;
            if (strCity.Length > 0) 
            {
                strCity = strCity.Remove(strCity.Length - 1, 1);

                string[] strCities = strCity.Replace("'", "").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string city in strCities) 
                {
                    string temp_yAxis = string.Empty;
                    for (int i = 0; i < nday; i++) 
                    {
                        System.Data.DataRow[] rows = dsCustomers.Tables[0].Select("city='" + city + "' and day = '"+startday.AddDays(i).ToString("yyyy-MM-dd")+"'");
                        if (rows.Length > 0)
                        {
                            temp_yAxis += rows[0]["count"].ToString()+",";
                        }
                        else 
                        {
                            temp_yAxis += "0,";
                        }
                    }

                    if (temp_yAxis.Length > 0)
                    {
                        temp_yAxis = temp_yAxis.Remove(temp_yAxis.Length - 1, 1);
                    }

                    yAxis += "{type: 'spline', name:'" + city + "', data: [" + temp_yAxis + "] },";
                }

                if (yAxis.Length > 0) 
                {
                    yAxis = yAxis.Remove(yAxis.Length - 1, 1);
                }
            }

            return new[] { xAxis, yAxis };
        }
    }
}